package com.ss.android.article.base.ui;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CircularImageView extends ImageView {
    private static final ImageView.ScaleType a = ImageView.ScaleType.CENTER_CROP;
    private static final Bitmap.Config b = Bitmap.Config.ARGB_8888;
    private final RectF c;
    private final RectF d;
    private final Matrix e;
    private final Paint f;
    private final Paint g;
    private final Paint h;
    private int i;
    private int j;
    private Bitmap k;
    private BitmapShader l;
    private int m;
    private int n;
    private float o;
    private float p;
    private boolean q;
    private boolean r;
    private int s;
    private Resources t;


    public CircularImageView(Context arg3) {
        this(arg3, null, 0);
    }

    public CircularImageView(Context arg4, AttributeSet arg5, int arg6) {
        super(arg4, arg5, arg6);
        this.c = new RectF();
        this.d = new RectF();
        this.e = new Matrix();
        this.f = new Paint();
        this.g = new Paint();
        this.h = new Paint();
        this.i = -16777216;
        this.j = 0;
        super.setScaleType(CircularImageView.a);
        if(arg5 != null) {
            TypedArray v0 = arg4.obtainStyledAttributes(arg5, com.ss.android.article.base.a.h, arg6, 0);
            this.j = v0.getDimensionPixelSize(com.ss.android.article.base.a.i, 0);
            v0.recycle();
        }

        this.t = arg4.getResources();
        this.s = Color.argb(128, 0, 0, 0);
        this.i = this.t.getColor(com.ss.android.article.browser.R.color.ssxinmian4);
        this.q = true;
        if(this.r) {
            this.a();
            this.r = false;
        }
    }

    public CircularImageView(Context arg2, AttributeSet arg3) {
        this(arg2, arg3, 0);
    }

    private void a() {
        float v1;
        float v2;
        float v8 = 2f;
        float v7 = 0.5f;
        float v0 = 0f;
        if(!this.q) {
            this.r = true;
        }
        else if(this.k != null) {
            this.l = new BitmapShader(this.k, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            this.f.setAntiAlias(true);
            this.f.setShader(this.l);
            this.h.setAntiAlias(true);
            this.h.setStyle(Paint.Style.FILL);
            this.h.setColor(this.s);
            this.g.setStyle(Paint.Style.STROKE);
            this.g.setAntiAlias(true);
            this.g.setColor(this.i);
            this.g.setStrokeWidth(((float)this.j));
            this.n = this.k.getHeight();
            this.m = this.k.getWidth();
            this.d.set(0f, 0f, ((float)this.getWidth()), ((float)this.getHeight()));
            this.p = Math.min((this.d.height() - (((float)this.j))) / v8, (this.d.width() - (((float)this.j))) / v8);
            this.c.set(((float)this.j), ((float)this.j), this.d.width() - (((float)this.j)), this.d.height() - (((float)this.j)));
            this.o = Math.min(this.c.height() / v8, this.c.width() / v8);
            this.e.set(null);
            if((((float)this.m)) * this.c.height() > this.c.width() * (((float)this.n))) {
                v2 = this.c.height() / (((float)this.n));
                v1 = (this.c.width() - (((float)this.m)) * v2) * v7;
            }
            else {
                v2 = this.c.width() / (((float)this.m));
                v1 = 0f;
                v0 = (this.c.height() - (((float)this.n)) * v2) * v7;
            }

            this.e.setScale(v2, v2);
            this.e.postTranslate(((float)((((int)(v1 + v7))) + this.j)), ((float)((((int)(v0 + v7))) + this.j)));
            this.l.setLocalMatrix(this.e);
            this.invalidate();
        }
    }

    private static Bitmap a(Drawable arg7) {
        Bitmap v0 = null;
        if(arg7 != null) {
            if((arg7 instanceof BitmapDrawable)) {
                return ((BitmapDrawable)arg7).getBitmap();
            }

            try {
                Bitmap v1_1 = (arg7 instanceof ColorDrawable) ? Bitmap.createBitmap(1, 1, CircularImageView.b) : Bitmap.createBitmap(arg7.getIntrinsicWidth(), arg7.getIntrinsicHeight(), CircularImageView.b);
                Canvas v2 = new Canvas(v1_1);
                arg7.setBounds(0, 0, v2.getWidth(), v2.getHeight());
                arg7.draw(v2);
                v0 = v1_1;
            }
            catch(OutOfMemoryError v1) {
            }
        }

        return v0;
    }

    public int getBorderColor() {
        return this.i;
    }

    public int getBorderWidth() {
        return this.j;
    }

    public ImageView.ScaleType getScaleType() {
        return CircularImageView.a;
    }

    protected void onDraw(Canvas arg5) {
        if(this.getDrawable() != null && this.j > 0) {
            arg5.drawCircle(((float)(this.getWidth() / 2)), ((float)(this.getHeight() / 2)), this.o, this.f);
            arg5.drawCircle(((float)(this.getWidth() / 2)), ((float)(this.getHeight() / 2)), this.p, this.g);
        }
    }

    protected void onSizeChanged(int arg1, int arg2, int arg3, int arg4) {
        super.onSizeChanged(arg1, arg2, arg3, arg4);
        this.a();
    }

    public void setBorderColor(int arg3) {
        if(arg3 != this.i) {
            this.i = arg3;
            this.g.setColor(this.i);
            this.invalidate();
        }
    }

    public void setBorderWidth(int arg2) {
        if(arg2 != this.j) {
            this.j = arg2;
            this.a();
        }
    }

    public void setImageBitmap(Bitmap arg1) {
        super.setImageBitmap(arg1);
        this.k = arg1;
        this.a();
    }

    public void setImageDrawable(Drawable arg2) {
        super.setImageDrawable(arg2);
        this.k = CircularImageView.a(arg2);
        this.a();
    }

    public void setImageResource(int arg2) {
        super.setImageResource(arg2);
        this.k = CircularImageView.a(this.getDrawable());
        this.a();
    }

    public void setScaleType(ImageView.ScaleType arg1) {
    }
}

